package com.neusoft.lohas.modules.ivm.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.modules.ivm.entity.order.DeliveryDetail;
import com.neusoft.lohas.modules.ivm.entity.order.Order;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class ExportDeliveryUtil{
	private Configuration configuration = null;

	public ExportDeliveryUtil() {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
	}

	public void createDoc(List<Order> orders, String fileUrl) {
		for(Order order:orders){
			//要填入模本的数据文件
			Map<String,Object> dataMap=new HashMap<String,Object>();
			getData(dataMap, order);
			//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
			//这里我们的模板是放在com.havenliu.document.template包下面
			configuration.setClassForTemplateLoading(this.getClass(), "/com/neusoft/lohas/modules/ivm/template");
			Template t=null;
			try {
				//test.ftl为要装载的模板
				t = configuration.getTemplate("delivery.ftl");
			} catch (IOException e) {
				e.printStackTrace();
			}
			//输出文档路径及名称
			File outFile = new File(fileUrl+".doc");
			Writer out = null;
			try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				t.process(dataMap, out);
			} catch (TemplateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	* 注意dataMap里存放的数据Key值要与模板中的参数相对应
	* @param dataMap
	*/
	private void getData(Map<String,Object> dataMap, Order order) {
		if(order.getPayType().equals("1")){
			order.setPayStatus("到付");
		}else{
			order.setPayStatus("网付");
		}
		dataMap.put("order", order);
	}
	
	/**
	 * 批量导出到一张word中
	 * @param orders
	 */
	public void createDocs(List<Order> orders, String fileUrl) {
		Map<String,Object> dataMap=new HashMap<String,Object>();
		for(Order order:orders){
			if(order.getPayStatus().equals("0")){
				order.setPayStatus("未付款");
			}else if(order.getPayStatus().equals("1")){
				order.setPayStatus("已付款");
			}
		}
		dataMap.put("orders", orders);
		//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
		//这里我们的模板是放在com.havenliu.document.template包下面
		configuration.setClassForTemplateLoading(this.getClass(), "/com/neusoft/lohas/modules/ivm/template");
		Template t=null;
		try {
			//test.ftl为要装载的模板
			t = configuration.getTemplate("deliveryList.ftl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		//输出文档路径及名称
		File outFile = new File(fileUrl+".doc");
		Writer out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			t.process(dataMap, out);
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ExportDeliveryUtil dh=new ExportDeliveryUtil();
		List<Order> list = new ArrayList<Order>();
//		dh.createDoc(list);
	}

}
